package cn.stylefeng.guns.modular.order.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.order.entity.TbFreight;
import cn.stylefeng.guns.modular.order.mapper.TbFreightMapper;
import cn.stylefeng.guns.modular.order.model.params.TbFreightParam;
import cn.stylefeng.guns.modular.order.model.result.TbFreightResult;
import  cn.stylefeng.guns.modular.order.service.TbFreightService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 运费表 服务实现类
 * </p>
 *
 * @author zhou
 * @since 2019-08-05
 */
@Service
public class TbFreightServiceImpl extends ServiceImpl<TbFreightMapper, TbFreight> implements TbFreightService {


    @Autowired
    private TbFreightMapper tbFreightMapper;

    @Override
    public void add(TbFreightParam param){
        tbFreightMapper.add(param);
    }

    @Override
    public void delete(TbFreightParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(TbFreightParam param){
        TbFreight oldEntity = getOldEntity(param);
        TbFreight newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public TbFreightResult findBySpec(TbFreightParam param){
        return null;
    }

    @Override
    public List<TbFreightResult> findListBySpec(TbFreightParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(TbFreightParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public TbFreightResult getFreightByAreaAndDestination(TbFreightParam param) {
        return tbFreightMapper.getFreightByAreaAndDestination(param);
    }

    @Override
    public TbFreightResult getWeightPriceById(Integer id) {
        return tbFreightMapper.getWeightPriceById(id);
    }

    @Override
    public LayuiPageInfo getWeightPriceList(TbFreightParam tbFreightParam) {
        Page pageContext = getPageContext();
        IPage page = tbFreightMapper.getWeightPriceList(pageContext, tbFreightParam);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(TbFreightParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private TbFreight getOldEntity(TbFreightParam param) {
        return this.getById(getKey(param));
    }

    private TbFreight getEntity(TbFreightParam param) {
        TbFreight entity = new TbFreight();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }


    @Override
    public TbFreightResult  freight(String area){
        return   tbFreightMapper.freight(area);
    }

    @Override
    public List<TbFreightResult> moenyByCity(TbFreightParam tbFreightParam) {
        return tbFreightMapper.moenyByCity(tbFreightParam);
    }
}
